const app = getApp();

const u = require("../../utils/common");

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    topicData: {
      type: Array,
    },
    total: {
      type: Number,
    },
    storageId: {
      type: String,
    },
    drawerActive: {
      type: Number || String,
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    scrollTop: 0,
    showDrawer: false,
    showPage: false,
    showAnswer: true,
    handleHidden: false,
    myList: null
  },

  /**
   * 组件的方法列表
   */
  methods: {
    toggleDrawer() {
      this.setData({
        showDrawer: !this.data.showDrawer,
        handleHidden: true,
      });
    },
    togglePage() {
      this.setData({
        showPage: !this.data.showPage,
        handleHidden: true,
      });
    },
    checkPage(e) {
      const { sort } = e.currentTarget.dataset;
      let { topicData, storageId } = this.properties;
      let myList = [topicData[sort - 1]];
      this.setData({
        drawerActive: sort,
        showPage: false,
        myList
      });

      wx.pageScrollTo({
        scrollTop: 0,
        duration: 300,
      });

      wx.setStorageSync(storageId + 'id', sort)
    },
    recite() {
      const that = this;
      const { showAnswer, handleHidden } = that.data;
      if (handleHidden) {
        that.setData({
          handleHidden: false,
        });
        return;
      }
      that.setData({
        showAnswer: !showAnswer,
      });
    },

    touch() {
      this.setData({
        handleHidden: true,
      });
    },

    clickList(e) {
      const { sort } = e.currentTarget.dataset;
      let { topicData, storageId } = this.properties;
      let myList = [topicData[sort - 1]];
      this.setData({
        drawerActive: sort,
        showDrawer: false,
        myList
      });

      wx.pageScrollTo({
        scrollTop: 0,
        duration: 300,
      });

      wx.setStorageSync(storageId + 'id', sort);
    },

    prev() {
      const { drawerActive, storageId, topicData } = this.properties;
      u.prev(this, drawerActive, storageId, topicData);
      wx.pageScrollTo({
        scrollTop: 0,
        duration: 300,
      });
    },
    next() {
      const { drawerActive, storageId, topicData } = this.properties;
      u.next(this, drawerActive, storageId, topicData);
      wx.pageScrollTo({
        scrollTop: 0,
        duration: 300,
      });
    },

    goToTop() {
      this.setData({
        scrollTop: 0,
      });
    },
  },
});
